package com.purengage.ipilih;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.purengage.ipilih.lib.DBClass;
import com.purengage.ipilih.lib.HttpConnect;
import com.purengage.ipilih.lib.Msg;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class Splash extends Activity{
	
	DBClass db;
	protected int _splashTime = 1830;
	private Thread splashTread;
	
	public class loadUrl extends AsyncTask<String, String, Boolean> {

		@Override
		protected Boolean doInBackground(String... params) {
			
			String dateModif = (db.getDateMsg(1).equalsIgnoreCase("")) ? "2013-1-1"
					: db.getDateMsg(1);

			String uri = "http://purengage.com/api/statuses?client_id="
					+ getResources().getString(R.string.client_id) + "&token="
					+ db.getValue("token") + "&to=all&indent=1"
					+ "&count=" + params + "&since_date_modified=" + dateModif.replaceAll("\\s+", "%20");

			HttpConnect http = new HttpConnect(uri);
			String output = "";

			boolean res = http.getData();
			if (res) {
				output = http.getOutput();

				try {
					JSONObject jobj = new JSONObject(output);
					JSONArray json = jobj.getJSONArray("data");

					for (int i = 0; i < json.length(); i++) {
						JSONObject job = json.getJSONObject(i);

						String user = (job.getString("from_volunteer_id")
								.equals("0") || job.getString(
								"from_volunteer_name").equalsIgnoreCase("null")) ? job
								.getString("from_user_name") : job
								.getString("from_volunteer_name");

						Msg m = new Msg();
						m.setId(job.getInt("id"));
						m.setMessage(job.getString("message"));
						m.setDate(job.getString("date_modified"));
						m.setUser(user);
						m.setMode(1);

						if (!job.getString("message").equalsIgnoreCase(""))
							db.insertMsg(m);

					}

				} catch (JSONException e) {
					e.printStackTrace();
				}

			}
			
			String dateInbox = (db.getDateMsg(2).equalsIgnoreCase("")) ? "2013-1-1"
					: db.getDateMsg(2);

			String url = "http://purengage.com/api/statuses?client_id="
					+ getResources().getString(R.string.client_id) + "&token="
					+ db.getValue("token") + "&to=me&indent=1" 
					+ "&count=" + params + "&since_date_modified=" + dateInbox.replaceAll("\\s+", "%20");

			HttpConnect conn = new HttpConnect(url);
			String out = "";

			boolean rs = conn.getData();
			if (rs) {
				out = conn.getOutput();
				try {
					JSONObject jobj = new JSONObject(out);
					JSONArray json = jobj.getJSONArray("data");

					for (int i = 0; i < json.length(); i++) {
						JSONObject job = json.getJSONObject(i);

						String user = (job.getString("from_volunteer_id")
								.equals("0") || job.getString(
								"from_volunteer_name").equalsIgnoreCase("null")) ? job
								.getString("from_user_name") : job
								.getString("from_volunteer_name");

						Msg m = new Msg();
						m.setId(job.getInt("id"));
						m.setMessage(job.getString("message"));
						m.setDate(job.getString("date_modified"));
						m.setUser(user);
						m.setMode(2);

						if (!job.getString("message").equalsIgnoreCase("")
								&& job.getString("to_volunteer_id")
										.equalsIgnoreCase(db.getValue("id")))
							db.insertMsg(m);

					}

				} catch (JSONException e) {
					e.printStackTrace();
				}

			}
			
			String dateGroup = (db.getDateMsg(3).equalsIgnoreCase("")) ? "2013-1-1"
					: db.getDateMsg(3);

			String urii = "http://purengage.com/api/statuses?client_id="
					+ getResources().getString(R.string.client_id) + "&token="
					+ db.getValue("token") + "&to=group&indent=1"
					+ "&count=" + params + "&since_date_modified=" + dateGroup.replaceAll("\\s+", "%20");

			HttpConnect cnn = new HttpConnect(urii);
			String ot = "";

			boolean result = cnn.getData();
			if (result) {
				output = cnn.getOutput();
				try {
					JSONObject jobj = new JSONObject(ot);
					JSONArray json = jobj.getJSONArray("data");

					for (int i = 0; i < json.length(); i++) {
						JSONObject job = json.getJSONObject(i);

						String user = (job.getString("from_volunteer_id")
								.equals("0") || job.getString(
								"from_volunteer_name").equalsIgnoreCase("null")) ? job
								.getString("from_user_name") : job
								.getString("from_volunteer_name");

						Msg m = new Msg();
						m.setId(job.getInt("id"));
						m.setMessage(job.getString("message"));
						m.setDate(job.getString("date_modified"));
						m.setUser(user);
						m.setMode(3);

						if (!job.getString("message").equalsIgnoreCase("")
								&& job.getString("id_prov").equalsIgnoreCase(
										db.getValue("id_prov"))
								&& job.getString("id_kab").equalsIgnoreCase(
										db.getValue("id_kab"))
								&& job.getString("kecamatan_id")
										.equalsIgnoreCase(
												db.getValue("kecamatan_id"))
								&& job.getString("desa_id").equalsIgnoreCase(
										db.getValue("desa_id")))
							db.insertMsg(m);

					}

				} catch (JSONException e) {
					e.printStackTrace();
				}

			}
			
			return null;
		}
		
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);

			finish();
			Intent i = new Intent();
			DBClass db = new DBClass(Splash.this);
			if(db.is_login())
				i.setClass(Splash.this, Main.class);
			else
				i.setClass(Splash.this, Login.class);
			
			startActivity(i);
				
		}
		
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash);

		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		splashTread = new Thread() {
			@Override
			public void run() {
				try {
					synchronized (this) {
						wait(_splashTime);
					}

				} catch (InterruptedException e) {
				} finally {
					finish();
					
					Intent i = new Intent();
					DBClass db = new DBClass(Splash.this);
					if(db.is_login())
						i.setClass(Splash.this, Main.class);
					else
						i.setClass(Splash.this, Login.class);
					
					startActivity(i);
				}
			}
		};
		
		splashTread.start();
    }			
	
}
